﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.IO;
using Commander.Classes;

namespace Commander.Views
{
    /// <summary>
    /// Клас для створення каталогу
    /// </summary>
    public partial class CreatFolder : Window
    {
        private string patch;
        private string NameCreateFolder;
        private Point old;

        public CreatFolder(string Patch)
        {
            InitializeComponent();
            patch = Patch;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //создать директорию
        private void Create_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NameCreateFolder = patch + NameFolder.Text;
                if (Directory.Exists(NameCreateFolder))
                {
                    MessageBoxResult result = MessageBox.Show(this, "Неможливо створити каталог " + NameFolder.Text + " так як він вже існує.\n Створити інший ккаталог ? ", "Commander", MessageBoxButton.YesNo, MessageBoxImage.Error);
                    if (result == MessageBoxResult.Yes)
                    {
                        NameFolder.Text = "";
                        Keyboard.Focus(NameFolder);
                    }
                    else this.Close();
                }
                else
                {
                    Directory.CreateDirectory(NameCreateFolder);                    
                    this.Close();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(NameFolder);
        }

        private void NameFolder_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Create_Click(sender, e);
            }
            else if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            old = e.GetPosition(null);
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point cur = e.GetPosition(null);
                this.Left += cur.X - old.X;
                this.Top += cur.Y - old.Y;
            }
        }
    }
}
